package org.karelun.gef;


import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ICoolBarManager;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.action.ToolBarContributionItem;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.karelun.gef.actions.CloseAction;
import org.karelun.gef.actions.CloseAllAction;
import org.karelun.gef.actions.CreateClassDiagramEditorAction;
import org.karelun.gef.actions.CreateSateEditorAction;
import org.karelun.gef.actions.CreateUseCaseEditorAction;
import org.karelun.gef.actions.EditorActionFactory;
import org.karelun.gef.actions.ExportImageAction;
import org.karelun.gef.actions.NewEditorActionGroup;
import org.karelun.gef.actions.OpenFileAction;
import org.karelun.gef.actions.ShowOutlineView;
import org.karelun.gef.actions.ShowPartAction;
import org.karelun.gef.actions.ShowPropertyAction;
import org.karelun.gef.helper.ConstantMessages;
import org.karelun.gef.helper.IDConstant;
import org.karelun.gef.helper.ImageResource;

public class ApplicationActionBarAdvisor extends ActionBarAdvisor {

	private IWorkbenchAction exitAction;
	private IWorkbenchAction aboutAction;
	private IWorkbenchAction undoAction;
	private IWorkbenchAction redoAction;
	private IWorkbenchAction deleAction;
	private IWorkbenchAction reSetAction;
	
	private IAction openFileAction;

	private IAction closeAction;
	private IAction closeAllAction;
	
	private IAction newClassEditorAction;
	private IAction newUserCaseEditorAction;
	private IAction newStateEditorAction;
	
	private NewEditorActionGroup newEditGroup;
	
	private IAction cutAction;
	private IAction copyAction;
	private IAction pasteAction;
	private IAction selectAllAction;
	
	private IWorkbenchAction saveAction;
	private IWorkbenchAction saveAsAction;
	
	private IAction exportImageAction;
	
	private IAction showPropertyViewAction;
	private IAction showOutlineAction;
	
	private IAction showPartAction;
	
	private IWorkbenchAction importAction;
	private IWorkbenchAction exportAction;
	
	private IWorkbenchAction addAttrAction;
	private IWorkbenchAction addMethodAction;
	
	public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
		super(configurer);
	}

	protected void makeActions(IWorkbenchWindow window) {
		newClassEditorAction=new CreateClassDiagramEditorAction(window);
		register(newClassEditorAction);
		
		newUserCaseEditorAction=new CreateUseCaseEditorAction(window);
		register(newUserCaseEditorAction);
		
		newStateEditorAction=new CreateSateEditorAction(window);
		register(newStateEditorAction);
		
		newEditGroup=new NewEditorActionGroup();
		newEditGroup.add(newClassEditorAction);
		newEditGroup.add(newUserCaseEditorAction);
		newEditGroup.add(newStateEditorAction);
		
		exitAction = ActionFactory.QUIT.create(window);
		register(exitAction);
		exitAction.setText(ConstantMessages.exit);

		reSetAction = ActionFactory.RESET_PERSPECTIVE.create(window);
		register(reSetAction);
		reSetAction.setText(ConstantMessages.resetViewer);
		ImageDescriptor imageDes = AbstractUIPlugin.imageDescriptorFromPlugin(Activator.PLUGIN_ID, ImageResource.REFRESH);
		reSetAction.setImageDescriptor(imageDes);

		aboutAction = ActionFactory.ABOUT.create(window);
		register(aboutAction);
		aboutAction.setText(ConstantMessages.about);

		undoAction = ActionFactory.UNDO.create(window);
		register(undoAction);
		undoAction.setText(ConstantMessages.Undo_Text);

		redoAction = ActionFactory.REDO.create(window);
		register(redoAction);
		redoAction.setText(ConstantMessages.Redo_Text);

		deleAction = ActionFactory.DELETE.create(window);
		register(deleAction);
		deleAction.setText(ConstantMessages.Delete_Text);
		
		cutAction=ActionFactory.CUT.create(window);
		register(cutAction);
		cutAction.setText(ConstantMessages.Cut_Text);

		copyAction = ActionFactory.COPY.create(window);
		register(copyAction);
		copyAction.setText(ConstantMessages.Copy_Text);

	    pasteAction=ActionFactory.PASTE.create(window);
		register(pasteAction);
		pasteAction.setText(ConstantMessages.Paste_Text);
		
		selectAllAction=ActionFactory.SELECT_ALL.create(window);
		register(selectAllAction);
		selectAllAction.setText(ConstantMessages.SelectAll_Text);
		
		openFileAction=new OpenFileAction(window);
		register(openFileAction);
		
		saveAction=ActionFactory.SAVE.create(window);
		saveAction.setText(ConstantMessages.saveFile);
		register(saveAction);

		saveAsAction=ActionFactory.SAVE_AS.create(window);
		saveAsAction.setText(ConstantMessages.saveAs_Text);
		register(saveAsAction);
		
		closeAction=new CloseAction(window);
		register(closeAction);
		
		closeAllAction=new CloseAllAction(window);
		register(closeAllAction);
		
		exportImageAction =new ExportImageAction(window);
		register(exportImageAction);
		
		showPropertyViewAction=new ShowPropertyAction(window);
		register(showPropertyViewAction);
		
		showOutlineAction=new ShowOutlineView(window);
		register(showOutlineAction);
		
		showPartAction=new ShowPartAction(window);
		register(showPartAction);
		
		importAction=ActionFactory.IMPORT.create(window);
		register(importAction);
		
		exportAction=ActionFactory.EXPORT.create(window);
		register(exportAction);
		
		addAttrAction=EditorActionFactory.ADDATTRIBUTE.create(window);
		register(addAttrAction);
		
		addMethodAction=EditorActionFactory.ADDMETHOD.create(window);
		register(addMethodAction);
	}

	protected void fillMenuBar(IMenuManager menuBar) {
		MenuManager fileMenu = new MenuManager(ConstantMessages.fileMenu, IDConstant.fileMenuId);
		MenuManager newEditorMenu=new MenuManager(ConstantMessages.newMenu,IDConstant.newMenuId);
		newEditorMenu.add(newClassEditorAction);
		newEditorMenu.add(newUserCaseEditorAction);
		newEditorMenu.add(newStateEditorAction);
		
		fileMenu.add(newEditorMenu);
		fileMenu.add(openFileAction);
		fileMenu.add(new Separator());
		fileMenu.add(closeAction);
		fileMenu.add(closeAllAction);
		fileMenu.add(new Separator());
		fileMenu.add(saveAction);
		fileMenu.add(saveAsAction);
		fileMenu.add(new Separator());
		fileMenu.add(exportImageAction);
		fileMenu.add(importAction);
		fileMenu.add(exportAction);
		fileMenu.add(new Separator());
		fileMenu.add(reSetAction);
		fileMenu.add(exitAction);

		MenuManager editMenu = new MenuManager(ConstantMessages.editMenu,IDConstant.editMenuId);
		editMenu.add(undoAction);
		editMenu.add(redoAction);
		editMenu.add(new Separator());
		editMenu.add(cutAction);
		editMenu.add(copyAction);
		editMenu.add(pasteAction);
		editMenu.add(deleAction);
		editMenu.add(new Separator());
		editMenu.add(selectAllAction);
		
		MenuManager insertMenu = new MenuManager(ConstantMessages.insertMenu_Text,IDConstant.insertMeneId);
		insertMenu.add(addAttrAction);
		insertMenu.add(addMethodAction);
		
		MenuManager viewMenu=new MenuManager(ConstantMessages.viewMenu,IDConstant.viewMenuId);
		viewMenu.add(showPropertyViewAction);
		viewMenu.add(showOutlineAction);
		viewMenu.add(showPartAction);

		MenuManager helpMenu = new MenuManager(ConstantMessages.helpMenu,IDConstant.helpMenuId);
		helpMenu.add(aboutAction);

		menuBar.add(fileMenu);
		menuBar.add(editMenu);
		menuBar.add(insertMenu);
		menuBar.add(viewMenu);
		menuBar.add(helpMenu);
	}
	
	@Override
	protected void fillCoolBar(ICoolBarManager coolBar) {
		IToolBarManager toolbar=new ToolBarManager(SWT.FLAT | SWT.RIGHT);
		toolbar.add(newEditGroup);
		toolbar.add(openFileAction);
		toolbar.add(saveAction);
    	coolBar.add(new ToolBarContributionItem(toolbar,"main")); //$NON-NLS-1$
	}
}
